package com.stu.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import com.stu.mall.common.utils.PageUtils;
import com.stu.mall.common.utils.Query;
import com.stu.mall.order.dao.OrderItemDao;
import com.stu.mall.order.entity.OrderItemEntity;
import com.stu.mall.order.service.OrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;


@RabbitListener(queues = "hello-java-queue")
@Service("orderItemService")
@Slf4j
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues 声明需要监听的queues
     */
    @RabbitListener(queues = "hello-java-queue")
    public void recieveMessage(Message message, OrderItemEntity orderItemEntity, Channel channel) throws IOException {
        log.info("接收到消息....内容....:" + message);
        log.info("接收到消息....类型....:" + message.getClass());

        log.info("接收到消息....内容....orderItemEntity:" + orderItemEntity);
        log.info("接收到消息....内容....Channel:" + channel);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        //签收货物，只签收当前货物，不批量签收
        channel.basicAck(deliveryTag,false);

        /**
         * deliveryTag 消息的ID
         * multiple=false:只拒收当前消息
         *requeue=false:拒收的消息，重新入队
         */
        channel.basicNack(deliveryTag,false,true);

    }

    /**
     * queues 声明需要监听的queues
     */

    @RabbitHandler
    public void recieveMessage(OrderItemEntity orderItemEntity) {
        log.info("接收到消息....内容....orderItemEntity:" + orderItemEntity);
    }
}
